package com.shucoin.www.tycoin;


import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by hl on 2017/6/9.
 */

public abstract class AbsServicexUtils  implements org.xutils.common.Callback.CommonCallback<String>{
    public Gson gson=new Gson();
    public  <E > void sendMsgToView(E e){
        if (e!=null)
        {
            EventBus.getDefault().post(e);
        }
    }
    public  <E > void sendMsgToView(Class<E> e,String json){
        E baseResponse=gson.fromJson(json, e);
        sendMsgToView(baseResponse);
    }

    public void sendToServer(RequestParams params){
        sendToServer(params,0);
    }
    public void sendToServer(RequestParams params,int post){
        sendToServer(params,post,false);
    }
    public void sendToServer(RequestParams params,int post,boolean isloading){

        params.setConnectTimeout(5000);
        if (post==1)
        {
            x.http().post(params,this);
        }else {
            x.http().get(params,this);
        }
//        if (isloading){
//            sendMsgToView(new BaseRequest());
//        }
            System.out.println("请求地址："+params.getUri());
            for (KeyValue keyValue : params.getQueryStringParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }
            for (KeyValue keyValue : params.getBodyParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }

    }

    @Override
    public void onSuccess(String result) {
            System.out.println("返回结果"+result);
    }




    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }

}
